
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #1e3a8a;
            --secondary: #10b981;
            --accent: #3b82f6;
            --light: #f8fafc;
            --dark: #0f172a;
            --gray: #94a3b8;
            --card-bg: rgba(255, 255, 255, 0.95);
        }

        body {
            background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
            color: var(--dark);
            min-height: 100vh;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            background: linear-gradient(90deg, var(--primary) 0%, #0c2043 100%);
            color: white;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        .header h1 {
            font-size: 1.8rem;
            font-weight: 700;
        }

        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 20px;
            font-size: 1rem;
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .user-table {
            width: 100%;
            background: var(--card-bg);
            border-radius: 10px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
            overflow: hidden;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
        }

        th {
            background-color: var(--primary);
            color: white;
            font-weight: 600;
        }

        tr:nth-child(even) {
            background-color: rgba(241, 245, 249, 0.5);
        }

        tr:hover {
            background-color: rgba(219, 234, 254, 0.5);
        }

        .action-btn {
            padding: 6px 12px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            font-size: 0.9rem;
            transition: all 0.3s ease;
            margin-right: 5px;
        }

        .edit-btn {
            background-color: var(--accent);
            color: white;
        }

        .edit-btn:hover {
            background-color: #2563eb;
        }

        .delete-btn {
            background-color: #ef4444;
            color: white;
        }

        .delete-btn:hover {
            background-color: #dc2626;
        }

        .add-user-btn {
            background-color: var(--secondary);
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 6px;
            font-size: 1rem;
            cursor: pointer;
            margin-top: 20px;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: all 0.3s ease;
        }

        .add-user-btn:hover {
            background-color: #0d9488;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            width: 90%;
            max-width: 500px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }

        .modal-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--primary);
        }

        .close-btn {
            background: none;
            border: none;
            font-size: 1.5rem;
            cursor: pointer;
            color: var(--gray);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: var(--dark);
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 1rem;
        }

        .form-actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }

        .save-btn {
            background-color: var(--primary);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
        }

        .cancel-btn {
            background-color: var(--gray);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            th, td {
                padding: 10px;
            }

            .header {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1><i class="fas fa-users"></i> 用户管理系统</h1>

        </div>

        <div class="user-table">
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>用户名</th>
                        <th>电话号码</th>
                        <th>密码</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="user-table-body">
                    <!-- 用户数据将通过JavaScript动态加载 -->
                </tbody>
            </table>
        </div>

        <button class="add-user-btn" id="add-user-btn">
            <i class="fas fa-plus"></i>
            <span>添加用户</span>
        </button>
    </div>

    <!-- 添加/编辑用户模态框 -->
    <div class="modal" id="user-modal">
        <div class="modal-content">
            <div class="modal-header">
                <h2 class="modal-title" id="modal-title">添加用户</h2>
                <button class="close-btn" id="close-modal">&times;</button>
            </div>
            <form id="user-form">
                <input type="hidden" id="user-id">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" required>
                </div>
                <div class="form-group">
                    <label for="phone">电话号码</label>
                    <input type="tel" id="phone" required>
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="text" id="password" required>
                </div>
                <div class="form-actions">
                    <button type="button" class="cancel-btn" id="cancel-btn">取消</button>
                    <button type="submit" class="save-btn" id="save-btn">保存</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 用户数据存储
        let users = [];
        let currentEditId = null;

        // DOM元素
        const userTableBody = document.getElementById('user-table-body');
        const addUserBtn = document.getElementById('add-user-btn');
        const backBtn = document.getElementById('back-btn');
        const userModal = document.getElementById('user-modal');
        const closeModalBtn = document.getElementById('close-modal');
        const cancelBtn = document.getElementById('cancel-btn');
        const userForm = document.getElementById('user-form');
        const modalTitle = document.getElementById('modal-title');
        const userIdInput = document.getElementById('user-id');
        const usernameInput = document.getElementById('username');
        const phoneInput = document.getElementById('phone');
        const passwordInput = document.getElementById('password');

        // 页面加载时获取用户数据
        document.addEventListener('DOMContentLoaded', () => {
            fetchUsers();
        });

        // 返回按钮点击事件
        backBtn.addEventListener('click', () => {
            window.location.href = 'index.html';
        });

        // 添加用户按钮点击事件
        addUserBtn.addEventListener('click', () => {
            currentEditId = null;
            modalTitle.textContent = '添加用户';
            userForm.reset();
            userModal.style.display = 'flex';
        });

        // 关闭模态框
        function closeModal() {
            userModal.style.display = 'none';
        }

        // 关闭按钮点击事件
        closeModalBtn.addEventListener('click', closeModal);
        cancelBtn.addEventListener('click', closeModal);

        // 表单提交事件
        userForm.addEventListener('submit', (e) => {
            e.preventDefault();

            const userData = {
                username: usernameInput.value,
                phone: phoneInput.value,
                password: passwordInput.value
            };

            if (currentEditId) {
                // 更新用户
                updateUser(currentEditId, userData);
            } else {
                // 添加用户
                addUser(userData);
            }

            closeModal();
        });

        // 从服务器获取用户数据
        function fetchUsers() {
            fetch('http://localhost:5001/get-users')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        users = data.users;
                        renderUserTable();
                    } else {
                        alert('获取用户数据失败: ' + data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    // 模拟数据 - 实际应用中应删除这部分
                    users = [
                        { id: 1, username: 'admin', phone: '13800000000', password: '123456' },
                        { id: 2, username: 'user1', phone: '13900000001', password: '123456' },
                        { id: 3, username: 'user2', phone: '13900000002', password: '123456' }
                    ];
                    renderUserTable();
                });
        }

        // 添加用户到服务器
        function addUser(userData) {
            fetch('http://localhost:5001/add-user', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(userData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    fetchUsers(); // 刷新用户列表
                } else {
                    alert('添加用户失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                // 模拟添加 - 实际应用中应删除这部分
                const newUser = {
                    id: users.length + 1,
                    ...userData
                };
                users.push(newUser);
                renderUserTable();
            });
        }

        // 更新用户信息
        function updateUser(id, userData) {
            fetch('http://localhost:5001/update-user', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    id: id,
                    ...userData
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    fetchUsers(); // 刷新用户列表
                } else {
                    alert('更新用户失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                // 模拟更新 - 实际应用中应删除这部分
                const index = users.findIndex(user => user.id == id);
                if (index !== -1) {
                    users[index] = { id: id, ...userData };
                    renderUserTable();
                }
            });
        }

        // 删除用户
        function deleteUser(id) {
            if (!confirm('确定要删除这个用户吗？')) return;

            fetch('http://localhost:5001/delete-user', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ id: id })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    fetchUsers(); // 刷新用户列表
                } else {
                    alert('删除用户失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                // 模拟删除 - 实际应用中应删除这部分
                users = users.filter(user => user.id != id);
                renderUserTable();
            });
        }

        // 渲染用户表格
        function renderUserTable() {
            userTableBody.innerHTML = '';

            users.forEach(user => {
                const row = document.createElement('tr');

                row.innerHTML = `
                    <td>${user.id}</td>
                    <td>${user.username}</td>
                    <td>${user.phone}</td>
                    <td>${user.password}</td>
                    <td>
                        <button class="action-btn edit-btn" data-id="${user.id}">
                            <i class="fas fa-edit"></i> 编辑
                        </button>
                        <button class="action-btn delete-btn" data-id="${user.id}">
                            <i class="fas fa-trash"></i> 删除
                        </button>
                    </td>
                `;

                userTableBody.appendChild(row);
            });

            // 添加编辑和删除按钮的事件监听
            document.querySelectorAll('.edit-btn').forEach(btn => {
                btn.addEventListener('click', (e) => {
                    const id = e.currentTarget.getAttribute('data-id');
                    editUser(id);
                });
            });

            document.querySelectorAll('.delete-btn').forEach(btn => {
                btn.addEventListener('click', (e) => {
                    const id = e.currentTarget.getAttribute('data-id');
                    deleteUser(id);
                });
            });
        }

        // 编辑用户
        function editUser(id) {
            const user = users.find(u => u.id == id);
            if (user) {
                currentEditId = id;
                modalTitle.textContent = '编辑用户';
                userIdInput.value = user.id;
                usernameInput.value = user.username;
                phoneInput.value = user.phone;
                passwordInput.value = user.password;
                userModal.style.display = 'flex';
            }
        }
    </script>
</body>
</html>